﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Entity;
using System.Data;

namespace Core.Dao
{
    class DownloadDao
    {
        public bool insert(Download download)
        {
            object[,] param = {
                {"@LanguageId", download.LanguageId},
                {"@Name", download.Name},
                {"@Desc", download.Desc},
                {"@Link", download.Link},
            };
            String sql = "insert into DOWNLOADS(PGL_ID, DWN_NAME, DWN_DESC, DWN_LINK) " +
                "values(@LanguageId, @Name, @Desc, @Link)";
            return DB.exec(sql, param);
        }

        public bool update(Download download)
        {
            object[,] param = {
                {"@LanguageId", download.LanguageId},
                {"@Name", download.Name},
                {"@Desc", download.Desc},
                {"@Link", download.Link},
                {"@id", download.Id},
            };
            String sql = "update DOWNLOADS set PGL_ID = @LanguageId, DWN_NAME = @Name, DWN_DESC = @Desc, DWN_LINK = @Link " +
                "where DWN_ID = @id";
            return DB.exec(sql, param);
        }

        public bool delete(int id)
        {
            object[,] param = {
                {"@id", id}   
            };
            String sql = "delete from DOWNLOADS where DWN_ID = @id";
            return DB.exec(sql, param);
        }

        public bool delete(String ids)
        {
            object[,] param = {
                {"@ids", ids}   
            };
            String sql = "delete from DOWNLOADS where DWN_ID in (" + ids + ")";
            return DB.exec(sql);
        }

        public DataTable get(int id)
        {
            object[,] param = {
                {"@id", id}   
            };
            String sql = "select * from DOWNLOADS where DWN_ID = @id";
            return DB.getData(sql, param);
        }

        public DataTable getAll()
        {
            String sql = "select do.*, la.PGL_NAME from DOWNLOADS do, PROGRAMMING_LANGUAGES la ";
            sql += "where do.PGL_ID = la.PGL_ID order by DWN_NAME";
            return DB.getData(sql);
        }
    }
}
